package com.qf.activiti.demo;


import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Thor
 * @公众号 Java架构栈
 */
public class TestActiviti {

    @Test
    public void test43(){
        //1.获取流程引擎对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    }



    /**
     * 部署操作：把bpmn文件部署到activiti当中
     */

    @Test
    public void test() {
        //1.获取流程引擎对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2. RunService  HistoryService TaskService RepositoryService(资源)
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //调用createDeployment方法创建部署对象，然后调用不部署方法
        Deployment deployment = repositoryService.
                createDeployment().
                addClasspathResource("bpmn/leaveContains.bpmn").
//                addClasspathResource("bpmn/leave.png").
                deploy();
        System.out.println("部署时间" + deployment.getDeploymentTime());
    }


    /**
     * 启动流程实例
     */
    @Test
    public void test1() {
        //1.获取流程引擎对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取运行的RunService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String,Object> map =new HashMap();
        map.put("day",4);
        //3.启动流程实例
        ProcessInstance leaveProcess = runtimeService.startProcessInstanceByKey("leaveContains",map);

    }

    /**
     * 查看代办任务
     */
    @Test
    public void test2() {

        //1.获取流程引擎对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取TaskService
        TaskService taskService = defaultProcessEngine.getTaskService();
        //3/查看代办任务
        // act_ru_task    select * from act_ru_task where assignee ="张三"
        List<Task> list = taskService.createTaskQuery().taskAssignee("张三").list();
        for (Task task : list) {
            System.out.println("任务节点名称：" + task.getName());
            System.out.println("节点审批人：" + task.getAssignee());
        }
    }


    @Test
    public void test3() {
        //1.获取流程引擎对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取TaskService
        TaskService taskService = defaultProcessEngine.getTaskService();
        //3.处理
        // act_ru_task    select * from act_ru_task where assignee ="张三"
        List<Task> list = taskService.createTaskQuery().taskAssignee("李四").list();
        for (Task task : list) {
            taskService.complete(task.getId());
            System.out.println("任务节点名称：" + task.getName());
            System.out.println("节点审批人：" + task.getAssignee());
        }

    }

    /**
     * 在审批任务的时候,添加审批意见
     */

    @Test
    public void test4() {
        //1.获取流程引擎对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取TaskService
        TaskService taskService = defaultProcessEngine.getTaskService();
        //3.处理
        // act_ru_task    select * from act_ru_task where assignee ="张三"
        List<Task> list = taskService.createTaskQuery().taskAssignee("张三").list();
        for (Task task : list) {
            //添加批注
            /**
             * 第一个参数:任务id
             * 第二个参数:流程实例的id
             * 第三个参数：批注的信息
             */
            taskService.addComment(task.getId(), task.getProcessInstanceId(), "同意");
            taskService.complete(task.getId());
            System.out.println("任务节点名称：" + task.getName());
            System.out.println("节点审批人：" + task.getAssignee());
        }
    }


    /**
     * 查看历史信息
     */
    @Test
    public void test5() {
        //1.获取流程引擎对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取HistoryService
        HistoryService historyService = defaultProcessEngine.getHistoryService();
        TaskService taskService = defaultProcessEngine.getTaskService();
        //3.查看历史节点信息
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
//                .taskAssignee("张三")
                .activityType("userTask")
                .list();
        for (HistoricActivityInstance historicActivityInstance : list) {
            System.out.println("-----------------------------------------------------");
            System.out.println("节点名称："+historicActivityInstance.getActivityName());
            System.out.println("节点审批开始时间："+historicActivityInstance.getStartTime());
            System.out.println("节点审批结束时间："+historicActivityInstance.getEndTime());
            //查看批注
            List<Comment> taskComments = taskService.getTaskComments(historicActivityInstance.getTaskId());
            if(taskComments !=null && taskComments.size()>0){
                System.out.println("批注信息为："+taskComments.get(0).getFullMessage());
            }
        }
    }


    @Test
    public void test6(){
        //1.获取流程引擎对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取HistoryService
        HistoryService historyService = defaultProcessEngine.getHistoryService();
        TaskService taskService = defaultProcessEngine.getTaskService();

        //3.查看历史节点信息
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().list();
        for (HistoricTaskInstance historicTaskInstance : list) {
            System.out.println("-----------------------------------------------------");
            System.out.println("节点名称："+historicTaskInstance.getName());
            System.out.println("节点审批开始时间："+historicTaskInstance.getStartTime());
            System.out.println("节点审批结束时间："+historicTaskInstance.getEndTime());

        }
    }



}
